﻿using DataProcess.Core.Tools;
using DataProcess.Interface;
using DataProcess.Interface.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace DataTransmission.WebApi
{
    public class DataTransmission : BaseDataTransmission
    {
        protected override string TransmissionMark { get { return "WebApi"; } }

        public DataTransmission(Config config) : base(config)
        {
            TransMissonConfig = config;
        }

        protected override Tuple<bool, string> SendData(TransferDataItem transDataItem)
        {
            var config = (Config)TransMissonConfig;
            if (config == null)
            {
                return new Tuple<bool, string>(false, $"{PreFix}加载Config失败");
            }
            else
            {
                string content = GetJsonContent(transDataItem);
                if (!string.IsNullOrEmpty(content))
                {
                    HttpHelper http = new HttpHelper();
                    var result = http.SendData(config.HttpEndpoint, content, config.Author, config.HttpRequestType);
                    http.Dispose();
                    return result;
                }
                else
                {
                    return new Tuple<bool, string>(false, "要传输的数据为空");
                }
            }
        }
    }

    public class Config : IDataTransmissionConfig
    {
        [Display(Name = "显示名称")]
        public string Name { get; set; }

        /// <summary>
        /// 是否向HttpEndpoint发送数据
        /// </summary>
        [Display(Name = "是否启用")]
        public bool Enabled { get; set; }

        /// <summary>
        /// 传输数据的格式
        /// </summary>
        [Display(Name = "传输数据格式")]
        public TransmissionDataFormat TransmissionDataFormat { get; set; } = TransmissionDataFormat.ProcessedData;

        /// <summary>
        /// Http请求方式
        /// </summary>
        [Display(Name = "HTTP请求方式(*)")]
        [Required(ErrorMessage = "[HTTP请求方式]必须选择")]
        public HttpRequestType HttpRequestType { get; set; }

        /// <summary>
        /// Http终结点
        /// </summary>
        [Display(Name = "HttpEndpoint(*)")]
        [Required(ErrorMessage = "[HttpEndpoint]必须填写")]
        public string HttpEndpoint { get; set; }

        /// <summary>
        /// 标头中的 Authorization
        /// </summary>
        [Display(Name = "授权码")]
        [PasswordPropertyText(true)]
        public string Author { get; set; }

        /// <summary>
        /// 失败重试发送。如果为False，那么发送失败数据也不会保存。
        /// </summary>
        [Display(Name = "失败重试发送")]
        [Description("如果为False，那么发送失败数据也不会保存。")]
        public bool ReTry { get; set; }

        [Display(Name = "完全失败数据是否保留")]

        public bool FaultDataRetention { get; set; }

        /// <summary>
        /// 重试次数。如果为0，那么每隔重试间隔时间，就尝试一次，直至成功。为其他数字则每隔重试间隔时间，就尝试一次，如果在重试次数耗尽还未传输成功，那么会将该条记录从发送失败表中删除不再传输。
        /// </summary>
        [Display(Name = "重试次数")]
        [Description("如果为0，那么每隔重试间隔时间，就尝试一次，直至成功。为其他数字则每隔重试间隔时间，就尝试一次，如果在重试次数耗尽还未传输成功，那么会将该条记录从发送失败表中删除不再传输。")]
        public int ReTryCount { get; set; } = 0;

        [Display(Name = "重试间隔(秒)")]
        [Range(60, 86400, ErrorMessage = "重试间隔(秒)必须在60-86400之间")]
        public int ReTyrInterval { get; set; } = 600;

        [Display(AutoGenerateField = false)]
        public string Message { get; set; }

        /// <summary>
        /// 判断此配置是否已经在指定的列表中
        /// </summary>
        /// <param name="collecion">指定的列表</param>
        /// <returns>是否已经存在</returns>
        public bool BeIncluded(IList<IDataTransmissionConfig> collecion, ref int index)
        {
            for (int i = 0; i < collecion.Count; i++)
            {
                if (collecion[i].ToString() == ToString())
                {
                    index = i + 1;
                    return true;
                }
            }
            return false;
        }

        public bool Valided()
        {
            if (string.IsNullOrEmpty(HttpEndpoint))
            {
                Message = "[HttpEndpoint]不能为空";
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            return $"{HttpRequestType}{HttpEndpoint}";
        }
    }
}
